<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
//use think\console\input\Argument;
//use think\console\input\Option;
use think\console\Output;
use think\facade\Cache;
use think\facade\Log;

/**
 * 同步成员、部门 每天一次
 * Class Synuser
 * @package app\command
 */
class Synuser extends Command
{

    protected function configure()
    {
        // 指令配置
        $this->setName('synuser')
            ->setDescription('syn synuser info to local');
    }

    protected function execute(Input $input, Output $output)
    {
        if(Cache::get('is_synuser') !== true || !Cache::get('is_synuser')){
            try {
                Cache::set('is_synuser',true,3600);
                $synDepartment = app()->invoke(['app\msgapi\service\HsorganizationService','synDepartment']);
                $synDepartment? Log::write('同步部门成功','info') : Log::write('同步部门失败','error');
                $synDepartment?$output->writeln('同步部门成功') : $output->writeln('同步部门失败');
                $synMember = app()->invoke(['app\msgapi\service\HsorganizationService','synMember']);
                $synMember?Log::write('同步成员成功','info') : Log::write('同步成员失败','error');
                $synMember?$output->writeln('同步成员成功') : $output->writeln('同步成员失败');
                Cache::set('is_synuser',false,3600);
            }catch (\Exception $e){
                Cache::set('is_synuser',false,3600);
                Log::write('同步成员、部门失败,'.$e->getMessage().$e->getLine(),'error');
                $output->writeln('同步成员、部门失败,'.$e->getMessage().$e->getLine());
            }
        }else{
            Log::write('通讯录同步程序已在执行中,请等待执行完成.','error');
            $output->writeln('通讯录同步程序已在执行中,请等待执行完成.');
        }
    }


}
